package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;
    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;
    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

	@Override
    public List<JSONObject> getBaseCategoryList() {
		//1、使用BaseCategoryViewMapper查询所有的三级分类信息(每个三级分类包含了自己 自己的二级、一级分类信息
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//2、将上面查询到的数据组装 为页面要的数据
		//2.1 一级分类集合
		List<JSONObject> list = new ArrayList<>();
		 //stream流groupby： 对1级分类id分组
		Map<Long, List<BaseCategoryView>> map1 = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//2.2先遍历一级分类
		map1.forEach((c1Id, views1) -> {
			JSONObject cate1 = new JSONObject();
			cate1.put("categoryId",c1Id);
			cate1.put("categoryName",views1.get(0).getCategory1Name());
			//2.3 二级分类集合
			List<JSONObject> cate2List = new ArrayList<>();
			Map<Long, List<BaseCategoryView>> map2 = views1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//因为每个二级分类都要遍历，所以先遍历二级分类。categoryChild不好直接封装
			map2.forEach((c2Id, views2) -> {
				//遍历一次，对应一个二级分类的对象
				JSONObject cate2 = new JSONObject();
						cate2.put("categoryId",c2Id);
						cate2.put("categoryName",views2.get(0).getCategory2Name());
			//2.4三级分类集合
				List<JSONObject> cate3List = views2.stream().map(view3 -> {
					JSONObject cate3 = new JSONObject();
					cate3.put("categoryId", view3.getCategory3Id());
					cate3.put("categoryName", view3.getCategory3Name());
					return cate3;
				}).collect(Collectors.toList());
						//
						cate2.put("categoryChild",cate3List);
						cate2List.add(cate2);
			});

					cate1.put("categoryChild",cate2List);
					list.add(cate1);

		});

        return list;
    }

	@Override
	public List<BaseAttribute> findAttribute(Long c1Id) {
		//因为使用了联查，一般需要自定义sql(自定义mapper)
		return	baseAttributeMapper.selectAttributesByC1Id(c1Id);

	}

    @Override
    public List<BaseCategory3> findCategory3sByC1Id(Long c1Id) {
		//1、根据1级分类id查询二级分类id集合
		List<Long> c2Ids = baseCategory2Mapper.selectList(Wrappers.lambdaQuery(BaseCategory2.class)
						.eq(BaseCategory2::getCategory1Id, c1Id)
						.select(BaseCategory2::getId))
				.stream()
				.map(BaseCategory2::getId)
				.collect(Collectors.toList());
		//2、根据二级分类id集合查询3级分类 前7个
		List<BaseCategory3> category3s = baseCategory3Mapper.selectList(Wrappers.lambdaQuery(BaseCategory3.class)
				.eq(BaseCategory3::getIsTop, 1) //判断是否需要首页展示
				.in(BaseCategory3::getCategory2Id, c2Ids)
				.last(" limit 7 "));
		return category3s;
	}

	@Override
	public JSONObject getBaseCategoryListByC1Id(Long c1Id) {
		//返回1级分类对象： categoryName  categoryId categoryChild
		//1、查询一级分类对象
		BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(c1Id);
		JSONObject jsonObject1 = new JSONObject();
		jsonObject1.put("categoryId",baseCategory1.getId());
		jsonObject1.put("categoryName",baseCategory1.getName());

		//2、查询1级分类的二级分类集合
		List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(Wrappers.lambdaQuery(BaseCategory2.class)
				.eq(BaseCategory2::getCategory1Id, c1Id));
		List<JSONObject> jsonObjects2 = new ArrayList<>();
		//3、遍历二级分类查询它所有的三级分类集合
		baseCategory2s.forEach(baseCategory2 -> {
			List<BaseCategory3> baseCategory3s = baseCategory3Mapper.selectList(Wrappers.lambdaQuery(BaseCategory3.class)
					.eq(BaseCategory3::getCategory2Id, baseCategory2.getId()));
			JSONObject jsonObject2 = new JSONObject();
			jsonObject2.put("categoryId",baseCategory2.getId());
			jsonObject2.put("categoryName",baseCategory2.getName());
			List<JSONObject> jsonObjects3 = baseCategory3s.stream().map(baseCategory3 -> {
				JSONObject jsonObject3 = new JSONObject();
				jsonObject3.put("categoryId", baseCategory3.getId());
				jsonObject3.put("categoryName", baseCategory3.getName());
				return jsonObject3;
			}).collect(Collectors.toList());

			jsonObject2.put("categoryChild",jsonObjects3);
			jsonObjects2.add(jsonObject2);
		});
		jsonObject1.put("categoryChild",jsonObjects2);
		return jsonObject1;
	}
}
